#include "filemanage.h"
#include <QDebug>

FileManage::FileManage(QObject *parent) :
    QObject(parent)
{

}

bool FileManage::copyFileToPath(QString sourceDir ,QString toDir, bool coverFileIfExist)
{
    toDir.replace("\\","/");
    if (sourceDir == toDir){
        return true;
    }
    if (!QFile::exists(sourceDir)){
        return false;
    }
    QDir *createfile     = new QDir;
    bool exist = createfile->exists(toDir);
    if (exist){
        if(coverFileIfExist){
            createfile->remove(toDir);
        }
    }//end if

    if(!QFile::copy(sourceDir, toDir))
    {
        return false;
    }
    return true;
}

bool FileManage::dir_search(QString mydir)
{
    QDir *temp = new QDir;
    bool exist = temp->exists(mydir);
    if(exist)
    {
       return true;
    }
    else
    {
        bool ok = temp->mkdir(mydir);
        if( ok )
        {
          return true;
        }else{
            return false;
        }
    }
}

bool FileManage::copyFile(int flag, int In_Out_flag,QString picname)
{
    QString currpath;
    QString map_name;
    QFile my_infile("in.jpg");
    QFile my_outfile("out.jpg");
    bool res;
    if (flag==1)
    {
        currpath="local/";
    }else if(flag==2)
    {
      currpath="temp/";
    }
    res=dir_search(currpath);
    if(In_Out_flag==1)
    {
        map_name="in.jpg";
        if(my_infile.exists() && res==true)
        {
            copyFileToPath(map_name,currpath+picname,res);
            qDebug()<<"copy success";
            my_infile.remove();
        }
    }else if(In_Out_flag==2)
    {
        map_name="out.jpg";
        if(my_outfile.exists() && res==true)
        {
            copyFileToPath(map_name,currpath+picname,res);
            qDebug()<<"copy success";
            my_outfile.remove();
        }
    }
}

QByteArray FileManage::openMyFile(int flag,QString picname)
{
    QString CurrPath;
    if (flag==1)
    {
        CurrPath="local/";
    }else if(flag==2)
    {
        CurrPath="temp/";
    }

    QFile myFile(CurrPath+picname);
    QByteArray PicBate;
    if (myFile.exists() && myFile.open(QIODevice::ReadOnly))
    {
        PicBate = myFile.readAll().toBase64();
        myFile.close();
        qDebug()<<"file open sucsess"<<picname;
        return PicBate;
    }
    else
    {
        PicBate="";
        qDebug()<<"file open fale";
        return PicBate;
    }
}

QByteArray FileManage::openMyDateFile(QString flag,QString picname)
{
    QString CurrPath;
    CurrPath=sysInfo->path+"/"+flag+"/";
    QByteArray PicBate;
    QFile myFile(CurrPath+picname);
    if (myFile.exists() && myFile.open(QIODevice::ReadOnly))
    {
        PicBate = myFile.readAll().toBase64();
        myFile.close();
       // qDebug()<<"FileManage::openMyDateFile sucsess "<<CurrPath+picname;
        return PicBate;
    }
    else
    {
        PicBate="";
       // qDebug()<<"FileManage::openMyDateFile false "<<CurrPath+picname;
        return PicBate;
    }
}

bool FileManage::MoveMyFile(QString flag, QString picname)
{
    /*QString CurrPath;
    if (flag==1)
    {
        CurrPath="local/";
    }else if(flag==2)
    {
        CurrPath="temp/";
    }
    QString filename=CurrPath+picname;
    QFile file(filename);
    file.remove();*/
    QString CurrPath;
    CurrPath=sysInfo->path+"/"+flag+"/";
    QString filename=CurrPath+picname;
    QFile file(filename);
    file.remove();
}

bool FileManage::MoveDateMyFile(QString flag, QString picname)
{
    QString CurrPath;
    CurrPath=flag+"/";
    QString filename=CurrPath+picname;
    QFile file(filename);
    file.remove();
}

bool FileManage::copyFileToDate(QString path, int In_Out_flag,QString picname)
{
    QString currpath;
    QString map_name;
    QFile my_infile("in.jpg");
    QFile my_outfile("out.jpg");
    bool res;
    currpath=sysInfo->path+"/"+path+"/";
    res=dir_search(currpath);
    if(In_Out_flag==1)
    {
        map_name="in.jpg";
        if(my_infile.exists() && res==true)
        {
            copyFileToPath(map_name,currpath+picname,res);
            qDebug()<<"FileManage::copyFileToDate success"<<map_name;
            my_infile.remove();
        }
    }else if(In_Out_flag==2)
    {
        map_name="out.jpg";
        if(my_outfile.exists() && res==true)
        {
            copyFileToPath(map_name,currpath+picname,res);
            qDebug()<<"FileManage::copyFileToDate success"<<map_name;
            my_outfile.remove();
        }
    }
}

void FileManage::removefilesindir(const QString& path)
{
    QString curPath;
    curPath=sysInfo->path+"/"+path;
    QDir dir(curPath);
    QFileInfoList info_list = dir.entryInfoList(QDir::Files | QDir::Hidden | QDir::NoDotAndDotDot | QDir::NoSymLinks | QDir::AllDirs);
    foreach(QFileInfo file_info, info_list)
    {
        if (file_info.isDir())
        {
            removefilesindir(file_info.absoluteFilePath());
        }
        else if (file_info.isFile())
        {
            QFile file(file_info.absoluteFilePath());
            qDebug() << "remove file  : " << file_info.absoluteFilePath();
            file.remove();
        }
    }
    QDir temp_dir;
    temp_dir.rmdir(curPath) ;
    qDebug() << "remove empty dir : " << curPath;
}

